package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.common.sc.Page;
import cn.buk.tms.dto.ServiceProductDto;
import cn.buk.tms.entity.misc.Product;
import cn.buk.tms.entity.misc.ProductBought;
import cn.buk.tms.entity.misc.TieInProduct;
import cn.buk.qms.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @author yfdai
 * @date 2017/5/26
 */
@RestController
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;


    @PostMapping(value = "/product")
    public JsonResult createInsurance(HttpServletRequest request,
                                          @RequestParam(value = "id", required = false, defaultValue = "0") int id,
                                          @RequestParam(value = "productType", required = false, defaultValue = "0") final int productType,
                                          @RequestParam(value = "productCode") final String productCode,
                                          @RequestParam(value = "productName") final String productName,
                                          @RequestParam(value = "productDesc", defaultValue = "") final String productDesc,
                                          @RequestParam(value = "price", defaultValue = "0") final int price,
                                          @RequestParam(value = "discount", defaultValue = "0") final int discount,
                                          @RequestParam(value = "cost", defaultValue = "0") final double cost,
                                          @RequestParam(value = "status", defaultValue = "1") final int status
                                      ) {
        final int enterpriseId = getEnterpriseId(request);

        TieInProduct p = new TieInProduct();
        p.setId(id);
        p.setEnterpriseId(enterpriseId);
        p.setProductType(productType);
        p.setProductCode(productCode);
        p.setProductName(productName);
        p.setProductDesc(productDesc);
        p.setPrice(price);
        p.setDiscount(discount);
        p.setCost(cost);
        p.setStatus(status);

        return productService.createTieInProduct(p);
    }

    @RequestMapping("/products")
    public CommonDto<ServiceProductDto> searchInsurances(HttpServletRequest request,
        @ModelAttribute("sc") CommonSearchCriteria sc,
        @RequestParam(value = "orderType", defaultValue = "0") final int orderType,
        @RequestParam(value = "productType", defaultValue = "-1") final int productType
        ) {
        final int enterpriseId = getEnterpriseId(request);

        List<ServiceProductDto> list = null;
        if (orderType == 1) {
            list = productService.searchRemoteServiceProducts(enterpriseId);
        } else {
            //本地系统的可用产品列表
            sc.setType(productType);
            list = productService.searchTieInProducts(enterpriseId, sc);
        }

        CommonDto<ServiceProductDto> dto = new CommonDto<>();
        dto.setDataList(list);
        dto.setPage(sc.getPage());

        return dto;
    }

    @RequestMapping("/product/{id}/delete")
    public JsonResult deleteProduct(HttpServletRequest request,
                                      @PathVariable("id") int id) {
        final int enterpriseId = getEnterpriseId(request);

        return productService.deleteProduct(enterpriseId, id);
    }

    @RequestMapping("/product/{id}")
    public ServiceProductDto searchProductById(HttpServletRequest request,
                                    @PathVariable("id") int id) {
        final int enterpriseId = getEnterpriseId(request);

        return productService.searchProductById(enterpriseId, id);
    }


    /**
     * 软件产品：指行程单脱机打印软件，比价等
     * @return
     */
    @RequestMapping("/soft-products")
    public CommonDto<Product> searchSoftProducts() {
        List<Product> list = productService.findAll();

        CommonDto<Product> dto = new CommonDto<>();
        dto.setDataList(list);
        dto.setPage(new Page());

        return dto;
    }

    /**
     * 软件产品：指行程单脱机打印软件，比价等
     * @return
     */
    @RequestMapping("/soft-product-buy-info")
    public ProductBought searchSoftProductBuyInfo(HttpServletRequest request,
                                                  @RequestParam("productCode") String productCode) {
        return productService.getProductBoughtByCode(getEnterpriseId(request), productCode);
    }


    /**
     * 购买软件产品：指行程单脱机打印软件，比价等
     * @return
     */
    @RequestMapping("/buy-soft-product")
    public JsonResult buySoftProduct(HttpServletRequest request,
                                                  @RequestParam("productCode") String productCode,
                                                  @RequestParam(value = "priceMode", defaultValue = "0") int priceMode) {
        return productService.buyProduct(getEnterpriseId(request), productCode, priceMode);
    }
}
